/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.config;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

/**
 * <p>Project: mybatis2021 - MybatisConfig
 * <p>Powered by webrx On 2021-07-23 10:41:30
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Configuration
@Import(DruidConfig.class)
@MapperScan(basePackages = "cn.webrx.mapper", sqlSessionFactoryRef = "sf")
public class MybatisConfig {
    @Bean
    public PageInterceptor interceptor(){
        return new PageInterceptor();
    }
    @Bean
    public SqlSessionFactoryBean sf(DataSource ds,PageInterceptor interceptor) throws IOException {
        var sf = new SqlSessionFactoryBean();
        sf.setDataSource(ds);
        sf.setTypeAliasesPackage("cn.webrx.entity");
        var resolver = new PathMatchingResourcePatternResolver();
        sf.setMapperLocations(resolver.getResources("classpath:mappers/*.xml"));
        sf.setPlugins(interceptor);
        return sf;
    }

    @Bean
    public SqlSessionTemplate session(SqlSessionFactory sf){
        var ss = new SqlSessionTemplate(sf);
        return ss;
    }
}
